import { messages } from "share/common";
export default {
  packingWidgetData(widget, index, languageList, currentLanguage) {
    if (widget.messageKey === "table") {
      widget.children &&
        widget.children.map((subWidget, index1) => {
          return this.packingWidgetData(subWidget, index1, languageList, currentLanguage);
        });

      console.log("widget.children", widget.children);
    }

    if (!widget.i18n) {
      widget.i18n = {};
    }

    if (widget.fieldNameI18n) {
      widget.i18n.fieldName = widget.fieldNameI18n;
    }
    if (widget.promptInfoI18n) {
      widget.i18n.promptInfo = widget.promptInfoI18n;
    }

    widget.sequence = index * 10;

    if (widget.messageKey === "linkage_switch") {
      widget.i18n.fieldContent = [];
      widget.customFormFieldI18nDTOS.map((i18nDTO) => {
        widget.i18n.fieldContent.push({
          language: i18nDTO.language,
          value: i18nDTO.fieldContent,
        });
      });
    }
    if (widget.messageKey === "select_box") {
      let temp = JSON.parse(widget.fieldContent);
      temp.length &&
        temp.map((box) => {
          if (!box.nameI18n) {
            box.nameI18n = [];
            languageList.map((language) => {
              box.nameI18n.push({
                language: language.code.toLowerCase(),
                value: currentLanguage.code.toLowerCase() === language.code.toLowerCase() ? box.name : "",
              });
            });
          }
        });
      widget.i18n.fieldContent = [];
      languageList.map((language) => {
        let mappingFieldContent = JSON.parse(JSON.stringify(temp));
        mappingFieldContent.map((box) => {
          box.nameI18n.map((subLanguage) => {
            if (subLanguage.language === language.code.toLowerCase()) {
              box.name = subLanguage.value;
            }
          });
        });
        widget.i18n.fieldContent.push({
          language: language.code,
          value: JSON.stringify(mappingFieldContent),
        });
      });
      widget.fieldContent = JSON.stringify(temp);
    }
    if (widget.messageKey === "number") {
      let temp = widget.fieldContent ? JSON.parse(widget.fieldContent) : { unit: "" };
      if (!temp.unitI18n) {
        temp.unitI18n = [];
        languageList.map((language) => {
          temp.unitI18n.push({
            language: language.code.toLowerCase(),
            value: currentLanguage.code.toLowerCase() === language.code.toLowerCase() ? temp.unit : "",
          });
        });
      }
      widget.i18n.fieldContent = [];
      languageList.map((language) => {
        let mappingFieldContent = JSON.parse(JSON.stringify(temp));
        mappingFieldContent.unitI18n.map((subLanguage) => {
          if (subLanguage.language === language.code.toLowerCase()) {
            mappingFieldContent.unit = subLanguage.value;
          }
        });
        widget.i18n.fieldContent.push({
          language: language.code,
          value: JSON.stringify(mappingFieldContent),
        });
      });
      widget.fieldContent = JSON.stringify(temp);
    }
    if (widget.messageKey === "start_date" || widget.messageKey === "end_date") {
      if (widget.fieldConstraint) {
        let temp = JSON.parse(widget.fieldConstraint);
        if (temp.enableTime === false) {
          temp.defaultTime = null;
        }
        widget.fieldConstraint = JSON.stringify(temp);
      }
    }
    if (widget.messageKey === "prompt_key") {
      widget.fieldName = "";
      widget.i18n.fieldName = [];
    }
    if (widget.messageKey === "venMaster" || widget.messageKey === "vendor_setting") {
      let temp = widget.fieldContent ? JSON.parse(widget.fieldContent) : { vendorScope: { range: "ALL", value: [] } };
      temp.visibleScope = temp.vendorScope && temp.vendorScope.range === "PARTIAL" ? 1002 : 1001;
      temp.idList = [];
      if (temp.vendorScope && temp.vendorScope.value && temp.vendorScope.value.length) {
        temp.vendorScope.value.forEach((oneVendor) => {
          temp.idList.push(oneVendor.id);
        });
      }
      temp.idList = temp.idList.join(",");
      widget.fieldContent = JSON.stringify(temp);
    }
    return widget;
  },

  processGroup(widgetList) {
    let isHasGroup = false;
    widgetList &&
      widgetList.forEach((widget) => {
        let fieldConstraint = widget.fieldConstraint ? JSON.parse(widget.fieldConstraint) : {};
        //把每个子项的默认值置空 fieldConstraint withoutDefaultValue
        fieldConstraint.withoutDefaultValue = true;

        if (widget.messageKey === "multi_linkage_cost_center" && widget.groupOID) {
          isHasGroup = true;

          //把linkage_cost_center的（显示模式、参与打印、参与费用分摊）塞入每个子项
          //显示模式 fieldConstraint withCode
          //参与打印 isPDFShow
          //参与费用分摊 fieldConstraint isApportionItem

          //fieldConstraint里子项不需要额外配置什么了，直接用外层的fieldConstraint即可
          widget.children &&
            widget.children.forEach((child) => {
              child.fieldConstraint = JSON.stringify(fieldConstraint);
              child.isPDFShow = widget.isPDFShow;

              //把groupOID塞入dataSource
              let dataSource = child.dataSource ? JSON.parse(child.dataSource) : {};
              dataSource.groupOID = child.groupOID;
              child.dataSource = JSON.stringify(dataSource);
            });
        }
      });
    if (isHasGroup) {
      let resultWidgetList = [];
      widgetList &&
        widgetList.forEach((widget) => {
          if (widget.messageKey === "multi_linkage_cost_center" && widget.groupOID) {
            if (widget.children && widget.children.length) {
              resultWidgetList.push(...widget.children);
            }
          } else {
            resultWidgetList.push(widget);
          }
        });
      return resultWidgetList;
    } else {
      return widgetList;
    }
  },

  initNowWidget(widgetList) {
    let groupMap = {};
    let widgetListExceptGroupChild = []; //不包含级联成本中心子组件的控件列表
    let zeroWidget = { insertGroupOIDList: [] }; //第0个widget的lastWidget
    let lastWidget = null; //下面列表循环的widget的上一个在 widgetListExceptGroupChild 中的widget
    widgetList &&
      widgetList.forEach((widget) => {
        let groupOID = widget.dataSource ? JSON.parse(widget.dataSource).groupOID : "";
        if (widget.messageKey === "select_cost_center" && groupOID) {
          widget.groupOID = groupOID;
          widget.subWidgetFlag = "Y"; //拖动的时候标记
          if (!groupMap[groupOID]) {
            groupMap[groupOID] = [];
          }
          groupMap[groupOID].push(JSON.parse(JSON.stringify(widget)));

          //标记插入级联成本中心的位置
          if (lastWidget) {
            if (!lastWidget.insertGroupOIDList) {
              lastWidget.insertGroupOIDList = [];
            }
            lastWidget.insertGroupOIDList.addIfNotExist(groupOID);
          } else {
            zeroWidget.insertGroupOIDList.addIfNotExist(groupOID);
          }
        } else {
          widgetListExceptGroupChild.push(widget);
          lastWidget = widget;
        }
      });

    let resultWidgetList = [];
    this.processGroupWidget(zeroWidget.insertGroupOIDList, groupMap, resultWidgetList);
    widgetListExceptGroupChild.forEach((widget) => {
      resultWidgetList.push(widget);
      this.processGroupWidget(widget.insertGroupOIDList, groupMap, resultWidgetList);
    });
    return resultWidgetList;
  },

  processGroupWidget(insertGroupOIDList, groupMap, resultWidgetList) {
    insertGroupOIDList &&
      insertGroupOIDList.forEach((groupOID) => {
        let newGroupWidget = {
          messageKey: "multi_linkage_cost_center",
          fieldName: messages("request-2.key513") /*级联成本中心*/,
          name: messages("request-2.key513") /*级联成本中心*/,
          groupOID,
          isGroupCostCenter: true,
          fieldConstraint: groupMap[groupOID][0].fieldConstraint,
          isPDFShow: groupMap[groupOID][0].isPDFShow,
          children: groupMap[groupOID],
          customFormFieldI18nDTOS: groupMap[groupOID][0].customFormFieldI18nDTOS, //防止被widget-setting认为是新拖拽的
        };
        resultWidgetList.push(newGroupWidget);
      });
  },
};
